package com.hutao.chargingpile.pitfall.controller;


import com.alibaba.excel.EasyExcel;
import com.hutao.chargingpile.pitfall.pojo.dto.ExcelCpHitchDTO;
import com.hutao.chargingpile.pitfall.pojo.entity.CpHitchDeal;
import com.hutao.chargingpile.pitfall.result.R;
import com.hutao.chargingpile.pitfall.service.CpHitchService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 故障表 前端控制器
 * </p>
 *
 * @author HuTao
 * @since 2023-03-22
 */
@Api(tags = "故障处理")
@RestController
@RequestMapping("/admin/cpHitch")
//@CrossOrigin
public class CpHitchController {
    @Resource
    private CpHitchService cpHitchService;

    @PostMapping("/findAll/{page}/{limit}/{startTime}/{endTime}/{siteName}/{rank}")
    public R kiss(
            @PathVariable Integer page, @PathVariable Integer limit,
            @PathVariable String startTime,@PathVariable String endTime,
            @PathVariable String siteName,@PathVariable String rank
    ){
        Map<String,Object> map = new HashMap<>();
        map.put("page",(page -1)*limit);
        map.put("limit",limit);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("siteName",siteName);
        map.put("rank",rank);
        int count = cpHitchService.count(map);
        return R.ok().datas("list",cpHitchService.findAll(map),count);
    }

    @PostMapping("/findChargingStations")
    public R findChargingStations(){return R.ok().data("options",cpHitchService.findChargingStations());}

    @PostMapping("/findCpHandleCode")
    public R findCpHandleCode(){return R.ok().data("code",cpHitchService.findCpHandleCode());}

    @PostMapping("/findHandlePerson")
    public R findHandlePerson(){return R.ok().data("code",cpHitchService.findHandlePerson());}

    @PostMapping("/findHitchById/{id}")
    public R findHitchById(@PathVariable String id){return R.ok().data("code",cpHitchService.findHitchById(id));}

    @PostMapping("/updateHitchById/{estimate}/{assContent}/{person}/{id}")
    public R updateHitchById(@PathVariable String estimate,@PathVariable String assContent,@PathVariable String person,@PathVariable String id){
        Map<String,Object> map = new HashMap<>();
        map.put("person",person);
        map.put("estimate",estimate);
        map.put("ass_content",assContent);
        map.put("id",id);
        System.out.println(estimate);
        if(cpHitchService.updateHitchById(map) > 0){
            return R.ok().message("ok");
        }else{
            return R.ok().message("no");
        }
    }

    @PostMapping("/returnPerson/{returnContent}/{id}")
    public R returnPerson(@PathVariable String returnContent,@PathVariable String id){
        Map<String,Object> map = new HashMap<>();
        map.put("returnContent",returnContent);
        map.put("id",id);
        if(cpHitchService.returnPerson(map) > 0){
            return R.ok().message("ok");
        }else{
            return R.ok().message("no");
        }
    }

    @PostMapping("/isHandle/{id}")
    public R isHandle(@PathVariable String id){
        if(cpHitchService.isHandle(id)){
            return R.ok().message("ok");
        }else{
            return R.ok().message("no");
        }
    }

    @PostMapping("/dealSave")
    public R dealSave(@RequestBody CpHitchDeal hitchDeal){
        System.out.println(hitchDeal.getExplain());
        if(cpHitchService.dealSave(hitchDeal)){
            return R.ok().message("ok");
        }else{
            return R.ok().message("no");
        }
    }

    @PostMapping("/isDeal/{id}")
    public R isDeal(@PathVariable Long id){
        if(cpHitchService.isDeal(id)){
            return R.ok().message("ok");
        }else{
            return R.ok().message("no");
        }
    }

    @PostMapping("/findDealByHitchId/{id}")
    public R findDealByHitchId(@PathVariable Long id){
        return R.ok().data("hitchInfo",cpHitchService.findDealByHitchId(id));
    }

    @PostMapping("/dealSaveNew")
    public R dealSaveNew(@RequestBody CpHitchDeal hitchDeal){
        if(cpHitchService.dealSaveNew(hitchDeal)){
            return R.ok().message("ok");
        }else{
            return R.ok().message("no");
        }
    }

    @PostMapping("/fetchDataById/{id}")
    public R fetchDataById(@PathVariable Long id){
        return R.ok().data("hitchInfo",cpHitchService.fetchDataById(id));
    }

    @PostMapping("/fetchDataDealById/{id}")
    public R fetchDataDealById(@PathVariable Long id){
        return R.ok().data("dealInfo",cpHitchService.fetchDataDealById(id));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        //这里注意，有同学使用swagger会导致各种问题，请直接使用浏览器或postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName= URLEncoder.encode("故障处理","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(), ExcelCpHitchDTO.class).sheet("故障处理").doWrite(cpHitchService.cpHitchExport());
    }
}

